package org.xwb.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureDemo02 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // getNow()，如果没算完，先拿个假值用着先，原来的计算还是继续
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 10;
        });
        System.out.println(future.getNow(30));

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(future.get());


        //complete ,只要打断成功，后续的值都是假的
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 100;
        });
        System.out.println(future01.complete(300));
        System.out.println(future01.get());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(future01.get());
    }
}
